﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathLib;
using System.Windows.Forms.DataVisualization.Charting;

namespace Demog {
    public partial class ChartForm : Form {
        private Series notFilteredSer1;
        private Series realSer1;
        private Series filteredSer1;
        private Series notFilteredSer2;
        private Series realSer2;
        private Series filteredSer2;
        private Matrix[] x;
        private Matrix[] y;
        private Matrix[] z;

        public ChartForm(Matrix[] x, Matrix[] y, Matrix[] z) {
            this.x = x;
            this.y = y;
            this.z = z;
            InitializeComponent();
            this.realSer1 = chart1.Series.Add("реальные данные");
            this.realSer2 = chart2.Series.Add("реальные данные");
            this.notFilteredSer1 = chart1.Series.Add("прогноз без фильтрации");
            this.notFilteredSer2 = chart2.Series.Add("прогноз без фильтрации");
            this.filteredSer1 = chart1.Series.Add("прогноз с фильтрацией");
            this.filteredSer2 = chart2.Series.Add("прогноз с фильтрацией");
            this.notFilteredSer1.ChartType = SeriesChartType.Column;
            this.filteredSer1.ChartType = SeriesChartType.Column;
            this.realSer1.ChartType = SeriesChartType.Column;
            this.notFilteredSer2.ChartType = SeriesChartType.Point;
            this.filteredSer2.ChartType = SeriesChartType.Line;
            this.realSer2.ChartType = SeriesChartType.Column;
            this.realSer2.Color = Color.LightBlue;
            this.filteredSer2.Color = Color.LightSeaGreen;
            this.notFilteredSer2.Color = Color.DarkOrange;
            this.notFilteredSer2.BorderWidth = 5;
            this.filteredSer2.BorderWidth = 5;
            Legend legend1 = chart1.Legends.Add("Legend");
            notFilteredSer1.Legend = filteredSer1.Legend = realSer1.Legend = "Legend";
            Legend legend2 = chart2.Legends.Add("Legend");
            notFilteredSer2.Legend = filteredSer2.Legend = realSer2.Legend = "Legend";

        }

        private void DrawPrediction(Matrix notFiltered, Matrix filtered, Matrix realdata, Chart chart1, int n, int var) {
            if (var == 2) {
                for (int i = 0; i < n; i++) {
                    notFilteredSer2.Points.AddXY(i + 1, notFiltered[i, 0]);
                    realSer2.Points.AddXY(i + 1, realdata[i, 0]);
                    filteredSer2.Points.AddXY(i + 1, filtered[i, 0]);
                }
            }
            if (var == 1) {
                for (int i = 0; i < n; i++) {
                    notFilteredSer1.Points.AddXY(i + 1, notFiltered[i, 0]);
                    realSer1.Points.AddXY(i + 1, realdata[i, 0]);
                    filteredSer1.Points.AddXY(i + 1, filtered[i, 0]);
                }
            }
        }



        private void Chart_Shown(object sender, EventArgs e) {


            notFilteredSer2.Points.Clear();
            filteredSer2.Points.Clear();
            realSer2.Points.Clear();
            Matrix a = new Matrix(this.x.Length, 1);
            Matrix b = new Matrix(this.y.Length, 1); 
            Matrix c = new Matrix(this.z.Length, 1);
            double s1 = 0;
            double s2 = 0;
            double s3 = 0;
            for (int i = 0; i < this.x.Length; i++) {
                for (int j = 0; j < this.x[0].NoRows; j++) { 
                     s1 += x[i][j,0];
                     s2 += y[i][j, 0];
                     s3 += z[i][j, 0];
                }

                a[i, 0] = s1;
                b[i, 0] = s2;
                c[i, 0] = s3;
                s1 = 0;
                s2 = 0;
                s3 = 0;
            }
            
            DrawPrediction(a, b, c, chart2, a.NoRows, 2);


            notFilteredSer1.Points.Clear();
            filteredSer1.Points.Clear();
            realSer1.Points.Clear();
            DrawPrediction(x[1], y[1], z[1], chart1, x[1].NoRows, 1);


        }

        private void chart1_Click(object sender, EventArgs e) {

        }

        private void label1_Click(object sender, EventArgs e) {

        }

        private void label2_Click(object sender, EventArgs e) {

        }
        private void bOK_Click(object sender, EventArgs e) {
            notFilteredSer1.Points.Clear();
            filteredSer1.Points.Clear();
            realSer1.Points.Clear();
            int step;
            try {
                step = int.Parse(tbStep.Text);
            } catch (Exception exe) {
                MessageBox.Show("Параметр введен некорректно", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
                DrawPrediction(x[step], y[step], z[step], chart1, x[step].NoRows, 1);
        }

      

        private void chart2_Click(object sender, EventArgs e) {

        }

    }
}
